package cn.hn.yh.controller.sjxh;

import cn.hn.yh.common.utils.DateUtil;
import cn.hn.yh.serviceInterface.sjxh.da.DaSjxhServiceInf;
import cn.hn.yh.vo.response.ReturnVO;
import cn.hn.yh.vo.response.SjxhDataVO;
import cn.hn.yh.vo.response.StateVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sjxh/da")
public class DaSjxhController {

    @Autowired
    private DaSjxhServiceInf daSjxhServiceInf;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private DateUtil dateUtil;

    @GetMapping("/getIsDaDeal")
    public String getDaDealInfo(String start, String end) {
        String str = "";
        Map<String, Object> map = new HashMap<>();
        List<String[]> list_date = new ArrayList<>();
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        List<Integer[]> result = daSjxhServiceInf.getDaDealInfo(start, end, dates);
        String[] d = new String[dates.size()];
        for (int i = 0; i < dates.size(); i++) {
            d[i] = dates.get(i);
        }
        list_date.add(d);
        try {
            map.put("xAxisList", list_date);
            map.put("data", result);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/getIsDaDealTips")
    public String getIsDaDealTips(String start, String end) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        String[] str_ = new String[3];
        Map<String, Object> map = new HashMap<>();
        List<String[]> list_date = new ArrayList<>();
        System.out.println("start==" + start);
        System.out.println("end===" + end);
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        List<Integer[]> result = daSjxhServiceInf.getDaDealInfo(start, end, dates);
        Integer cj = 0;
        Integer wcj = 0;
        for (int i = 0; i < result.get(0).length; i++) {
            if (result.get(0)[i] == 0) {
                wcj++;
            } else if (result.get(0)[i] == 1) {
                cj++;
            }
        }
        str_[0] = String.valueOf(dates.size());
        str_[1] = String.valueOf(cj);
        str_[2] = String.valueOf(wcj);
        list.add(str_);
        try {
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "1");
            map.put("data", list);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /*----------------------------------------------------------------------------------------*/

    @GetMapping("/vue_sjxh_table")
    public String getVueIsDaDealTips(String start, String end) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        String[] str_ = new String[3];
        Map<String, Object> map = new HashMap<>();
        List<String[]> list_date = new ArrayList<>();
        System.out.println("start==" + start);
        System.out.println("end===" + end);
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        List<Integer[]> result = daSjxhServiceInf.getDaDealInfo(start, end, dates);
        Integer cj = 0;
        Integer wcj = 0;
        for (int i = 0; i < result.get(0).length; i++) {
            if (result.get(0)[i] == 0) {
                wcj++;
            } else if (result.get(0)[i] == 1) {
                cj++;
            }
        }
        str_[0] = String.valueOf(dates.size());
        str_[1] = String.valueOf(cj);
        str_[2] = String.valueOf(wcj);
        list.add(str_);
        try {
            ReturnVO returnVO = new ReturnVO();
            StateVO stateVO = new StateVO();
            SjxhDataVO sjxhDataVO = new SjxhDataVO();
            List<Object> dataList = new ArrayList<>();
            stateVO.setCode(200);
            stateVO.setMsg("success");
            returnVO.setState(stateVO);
            sjxhDataVO.setTotal(str_[0]);
            sjxhDataVO.setDeal(str_[1]);
            sjxhDataVO.setNodeal(str_[2]);
            dataList.add(sjxhDataVO);
            returnVO.setData(dataList);
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_sjxh_chart1")
    public String getSjxhDaChart1(String start, String end) {
        String str = "";
        Map<String, Object> map = new HashMap<>();
        List<String[]> list_date = new ArrayList<>();
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        List<Integer[]> result = daSjxhServiceInf.getDaDealInfo(start, end, dates);
        String[] d = new String[dates.size()];
        for (int i = 0; i < dates.size(); i++) {
            d[i] = dates.get(i);
        }
        list_date.add(d);
        try {
            map.put("xAxisList", list_date);
            map.put("data", result);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_sjxh_chart2")
    public String getSjxhDaChart2(String start, String end) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        String[] str_ = new String[3];
        Map<String, Object> map = new HashMap<>();
        List<String[]> list_date = new ArrayList<>();
        System.out.println("start==" + start);
        System.out.println("end===" + end);
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        List<Integer[]> result = daSjxhServiceInf.getDaDealInfo(start, end, dates);
        Integer cj = 0;
        Integer wcj = 0;
        for (int i = 0; i < result.get(0).length; i++) {
            if (result.get(0)[i] == 0) {
                wcj++;
            } else if (result.get(0)[i] == 1) {
                cj++;
            }
        }
        str_[0] = String.valueOf(dates.size());
        str_[1] = String.valueOf(cj);
        str_[2] = String.valueOf(wcj);
        list.add(str_);
        try {
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "1");
            map.put("data", list);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
}
